import logging
import time

from appium.webdriver.common.touch_action import TouchAction

from tools.base_page import BasePage


class Course(BasePage):
    course_type_location = ('xpath', '//*[@text="课程"]')

    def search_successful(self, text_value):
        search_locator = ('id', 'search_edit_text')
        self.find(search_locator).click()
        self.find(search_locator).send_keys(text_value)
        self.swich_to_method()
        self.exist_search_res()

    def kaoyan_course(self):
        kaoyan = ('xpath', '//*[@text="考研"]')
        first_course_title = ('id', 'comm_item_course_title')
        self.find(kaoyan).click()

        self.elements(first_course_title)[1].click()
        self.back_activity(1)
        self.go_swipe(0.5, 0.8, 0.5, 0.2)
        time.sleep(1)
        logging.info('测试完毕')
        self.page(self.page())

    def more_type_course_search(self, text_value):
        kaoyan = ('xpath', '//*[@text="考研"]')
        zsb = ('xpath', '//*[@text="专升本"]')
        kb = ('xpath', '//*[@text="考公考编"]')
        cert = ('xpath', '//*[@text="资格证书"]')
        search_locator = ('id', 'toolbarSearch')
        input_search = ('id', 'search_edit_text')
        more_type = (kaoyan, zsb, kb, cert)
        for more in range(len(more_type)):
            self.find(more_type[more]).click()
            self.find(search_locator).click()
            self.find(input_search).send_keys(text_value)
            self.swich_to_method()
            self.exist_search_res()

    def swich_to_method(self):
        '''
        切换输入法
        :return:
        '''
        self.driver.activate_ime_engine('com.iflytek.inputmethod/.FlyIME')
        self.try_and_except()
        self.driver.activate_ime_engine('io.appium.settings/.UnicodeIME')

    def exist_search_res(self):
        search_res_title = ('id', 'comm_item_course_title')
        if self.is_element(search_res_title):
            logging.info("搜索结果存在，完成搜索")
            self.page(self.course_type_location)
        else:
            logging.info("搜索结果不存在！请重新输入")
            self.page(self.course_type_location)
        return True

    def my_collect_del(self):
        cellect = ('text', '我的收藏')
        cellect_type = ('id', 'indicatorTitleTv')
        img_locator = ('id', 'img')
        self.collect_job()
        self.find_element(cellect).click()
        self.elements(cellect_type)[0].click()
        ele = self.elements(img_locator)[0].click()
        self.page(self.index)
        # time.sleep(5)
        # TouchAction(self.driver).long_press(x=628, y=293).wait(2000).move_to(x=149, y=290).wait(1000).release().perform()

    def collect_job(self):
        self.find(self.index).click()
        kb = ('text', '考公考编')
        first_job = ('id', 'item_bulletin_job_title')
        collect = ('id', 'collectImg')
        course = ('text', '课程')

        self.find(self.index).click()
        self.find_element(kb).click()
        self.go_swipe(0.5, 0.8, 0.5, 0.2)
        self.elements(first_job)[0].click()
        self.find(collect).click()
        self.page(self.index)
        self.find_element(course).click()

    def open_course(self):
        time.sleep(2)
        self.go_swipe(0.5, 0.7, 0.5, 0.3)
        time.sleep(2)
        first_course_title = ('id', 'com.znb.zxx:id/commItemOpenCourseTitle')
        cat_more = ('id', 'com.znb.zxx:id/contentTv')
        more_first_title = ('id', 'com.znb.zxx:id/comm_item_course_title')
        self.elements(first_course_title)[0].click()
        self.back_activity(1)
        self.find(cat_more).click()
        self.elements(more_first_title)[0].click()
        self.page(self.index)

    def offline_course(self):
        self.go_swipe(0.5, 0.7, 0.5, 0.3)
        time.sleep(2)
